package com.bybops.ui.fragment

import android.os.Bundle
import android.view.View
import baseproject.com.mybaseproject.mvp.contract.NewDeviceInitFragmentContract
import baseproject.com.mybaseproject.ui.base.BaseMVPFragment
import baseproject.com.mybaseproject.utils.scheduler.RxUtils
import com.bybops.R
import com.bybops.app.MyApplication
import com.bybops.model.bean.LockDetail
import com.bybops.presenter.NewDeviceInitFragmentPresenterImpl
import com.bybops.ui.activity.NewDeviceUseActivity
import kotlinx.android.synthetic.main.fragment_new_device_init.*

/**
 * 新设备启用的启用界面
 */
class NewDeviceStartUseFragment : BaseMVPFragment<NewDeviceInitFragmentContract.View, NewDeviceInitFragmentPresenterImpl>()
        , NewDeviceInitFragmentContract.View {
    override var mPresenter: NewDeviceInitFragmentPresenterImpl = NewDeviceInitFragmentPresenterImpl()
    var mActivity: NewDeviceUseActivity? = null


    override fun getLayoutId(): Int? {
        return R.layout.fragment_new_device_init
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mActivity = activity as NewDeviceUseActivity
        initView()
        addListener()
    }

    private fun initView() {
        //隐藏箭头
        imDeviceArrow.visibility = View.GONE
        imLockArrow.visibility = View.GONE
        //设置数据
        tvDeviceId.text = mActivity?.mDeviceId
        tvLockId.text = mActivity?.mLockId
        etHospital.setText(mActivity?.mHospital)
        etAddress.setText(mActivity?.mAddress)
        tvDutyMan.text = mActivity?.mDailyMan

        etHospital.isFocusable = false
        etAddress.isFocusable = false
    }

    private fun addListener() {
        //启用按钮的点击事件
        RxUtils.rxClick(initButton , object : RxUtils.OnClickInterf{
            override fun onViewClick(view: View) {
                //调用启用新设备接口
                var params = HashMap<String, String?>()
                params["type"] = "2"
                params["equipSN"] = tvLockId.text.toString().trim()
                params["access_token"] = MyApplication.instance?.mAccessToken
                params["barcode"] = tvDeviceId.text.toString().trim()
                params["energy"] = mActivity?.mVoltage
                params["userKey"] = MyApplication.instance?.mUser?.userKey
                params["userName"] = tvDutyMan.text.toString().trim()
                params["hospital"] = etHospital.text.toString().trim()
                params["hospitalId"] = ""
                params["department"] = etAddress.text.toString().trim()
                mPresenter.activeDevice(params)
            }
        })
    }

    //获取到蓝牙锁的电量
    override fun setLockVoltage(voltage: String?) {

    }

    /**
     * 根据蓝牙锁id获取mac地址成功
     */
    override fun getDetailByBarCodeSuccess(data: LockDetail) {
    }

    /**
     * 初始化接口访问成功
     */
    override fun onActiveDeviceSuccess(data: LockDetail) {
        showToast("启用成功")
        mActivity?.finish()
    }

    override fun getDviceId(): String? {
        return tvDeviceId.text.toString().trim()
    }
}